
<template>
  <div>
    <uni-popup
    @maskClick="confirm"
    ref="popup" type="bottom" background-color="#fff">
      <uni-calendar
      :endDate="endDate"
      :startDate="startDate"
      @change="change"
      :selected="selected.data"
      ref="calendar"
      :insert="true"
      
    />
    </uni-popup>
  </div>
</template>
<script setup lang="ts">
import {ref,reactive,onMounted} from "vue";
import {qiandao ,qiandaoData}from '../../../../api/login'

import {addMissingDates} from '../../../../utils/date'
const $emit = defineEmits(['guan'])
const calendar = ref<any>(null);

const confirm = (e: any) => {
  $emit('guan')
  
};
// 签到
const dianjidao = async() => {
  
  const {data}= await qiandao(selected.data)
  getDao()

};
// 打卡接口
interface dao{
  date:string,
  info:string
}
interface rda{
  data:dao[]
}
let selected:rda =reactive({
  data:[]
}) 

// 获取签到数据
const getDao=async()=>{
  const {data}=await qiandaoData()
  
  if(data.data.name){
   
    selected.data=JSON.parse(data.data.name)
if(selected.data) addMissingDates(selected.data)
    
  }else{
    selected.data=[]
    
  }
  
  
}

function getDate(date: any, AddDayCount = 0) {
  if (!date) {
    date = new Date();
  }
  if (typeof date !== "object") {
    date = date.replace(/-/g, "/");
  }
  const dd = new Date(date);

  dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期

  const y = dd.getFullYear();
  const m =
    dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1; // 获取当前月份的日期，不足10补0
  const d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
  return {
    fullDate: y + "-" + m + "-" + d,
    year: y,
    month: m,
    date: d,
    day: dd.getDay(),
  };
}





const change =(e:any) =>{



// !selected.data.some(item=>item.date==e.fulldate
//   )
// 
  if(e.extraInfo.info=='补签'||
  e.fulldate==getDate(new Date()).fullDate&&e.extraInfo.info!=='已打卡'){

uni.showModal({
title: '提醒',
content: e.extraInfo.info?e.extraInfo.info:'今日签到',
success: function (res) {
  if (res.confirm) {

// 模拟动态打卡
// if (selected.data.length > 5) return;

if(e.extraInfo.info=="补签"){
  selected.data= selected.data.filter(item => {
 
   return item.date!==e.fulldate
  });

  
}
selected.data.push({
  date: e.fulldate,
  info: "已打卡",
});

dianjidao()
  } else if (res.cancel) {
    console.log('用户点击取消');
  }
}
});

  }





  
};
let endDate= getDate(new Date(),90).fullDate
let  startDate = ref(getDate(new Date(),-100).fullDate)
const popup:any= ref(null)
const opens=()=>{
  getDao()
  
  popup.value.open()
}
onMounted(()=>{
  opens()
})
</script>

<style scoped lang="scss">
:deep(.uni-calendar-item__weeks-lunar-text){
  color: #000 !important;
  background-color: aqua !important;
}
.uni-calendar-item__weeks-lunar-text{
  color: #000 !important;
  
}
::v-deep .uni-calendar-item--extra{
  color: #000 !important;
  background-color: aqua !important;
}
.uni-calendar-item--extra{
  color: #000 !important;
  background-color: aqua !important;
}
</style>